﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace Kaleta.Xna.Timing
{
    public class TimedObject
    {
        double total { get; set; }
        double timer { get; set; }
        bool repeating { get; set; }
        short repeated { get; set; }

        public double Delay { get; set; }
        public double EveryMillisecond {get;set; }
        public double Duration { get; set; }
        public TimeRepeat Behavior { get; set; }

        internal string Key { get; set; }

        /// <summary>
        /// Defines a timing object. Key parameter is a unique name within application.
        /// </summary>
        /// <param name="key"></param>
        public TimedObject(string key)
        {
            repeating = false;
            Key = key;
        }

        public TimedObject()
        {
            repeating = false;
            Key = this.GetHashCode().ToString();
        }


        public bool CanRun(GameTime gameTime, TimedObject obj)
        {
            return CanRun(gameTime, obj.EveryMillisecond, obj.Delay, obj.Duration, obj.Behavior);
        }

        public bool CanRun(GameTime gameTime, double everyMillisecond, double delay, double duration, TimeRepeat behavior)
        {


            if (duration > 0)
            {
                total += gameTime.ElapsedGameTime.TotalMilliseconds;

                if (delay < total && total <= duration + delay)
                {
                    repeating = true;
                    timer += gameTime.ElapsedGameTime.TotalMilliseconds;

                    if (timer > everyMillisecond)
                    {
                        if (everyMillisecond == 0)
                            timer = 0;
                        else
                            timer -= everyMillisecond;
                        return true;
                    }
                }
                else if (repeating == true)
                {
                    if (behavior == TimeRepeat.None)
                    { duration = -1; }
                    else if (behavior == TimeRepeat.Forever)
                    {
                        repeating = false;
                        total -= duration;
                        total -= delay;
                    }
                    else if (behavior == TimeRepeat.Once && repeated < 1)
                    {
                        repeated++;

                        repeating = false;
                        total -= duration;
                        total -= delay;
                    }
                    else if (behavior == TimeRepeat.Twice && repeated < 2)
                    {
                        repeated++;

                        repeating = false;
                        total -= duration;
                        total -= delay;
                    }
                    else if ((int)behavior > repeated && behavior != TimeRepeat.None
                        && behavior != TimeRepeat.Forever && behavior != TimeRepeat.Once &&
                        behavior != TimeRepeat.Twice)
                    {
                        repeated++;

                        repeating = false;
                        total -= duration;
                        total -= delay;
                    }

                }

            }
            else if (duration == 0)
            {
                timer += gameTime.ElapsedGameTime.TotalMilliseconds;

                if (timer - delay > everyMillisecond)
                {
                    if (everyMillisecond == 0)
                        timer = 0;
                    else
                        timer -= everyMillisecond;
                    return true;
                }
            }

            return false;
        }
    }
}
